package com.xiaomengkuan.user.controller;

import com.xiaomengkuan.common.entity.vo.PageVo;
import com.xiaomengkuan.common.util.DetailSelector;
import com.xiaomengkuan.common.util.R;
import com.xiaomengkuan.user.entity.vo.UserPageVo;
import com.xiaomengkuan.user.entity.vo.UserResponseVo;
import com.xiaomengkuan.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-02-05 2:58 下午, File Encoding: utf8 --
 */
@RestController
@RequestMapping("user/user")
@Api(tags = "用户管理")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 分页查询用户 每页10条记录
     *
     * @param current 当前页
     * @return R
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("page/{current}")
    public R all(@PathVariable("current") int current) {
        PageVo<UserResponseVo> page = userService.page(current);
        return R.ok().detail(DetailSelector.QUERY_SUCCESS).page(page);
    }

    @ApiOperation(value = "返回用户是否存在")
    @GetMapping("isExist/{id}")
    public R isExist(@PathVariable("id") String id) {
        boolean isExist = userService.isExist(id);
        return isExist ? R.ok().detail(DetailSelector.EXIST) :
                R.err().detail(DetailSelector.NOT_EXIST);
    }

    /**
     * 创建新用户
     *
     * @param requestBody map
     * @return R
     */
    @ApiOperation(value = "创建新用户")
    @PostMapping("newUser")
    public R newUser(@RequestBody Map<String, Object> requestBody) {
        String flag = userService.newUser(requestBody);
        return flag != null ?
                R.ok().detail(DetailSelector.INSERT_SUCCESS).putItem(flag) :
                R.err();
    }



}
